﻿var alertsetting = {
    icon: { error: "error", info: "info", warn: "warning" },
    buttontemplate: '<div class="simplebutton"><div class="r"><button type="button"></button></div></div>',
    template : '<div id="alert" class="alert">\
                        <div class="alertcontent">\
                            <div>\
                                <div class="alertinner"><h1></h1>\
                                    <div class="msg"></div>\
                                </div>\
                                <div class="toolbar"></div>\
                            </div>\
                        </div>\
                        <div class="alertfooter">\
                            <div class="alertfooter_r">\
                                <div class="alertfooter_c"></div>\
                            </div>\
                        </div>\
                     </div>'
};
var alertManager = function (app) {
    this.application = app;
    this.open = function (option) {
        option = $.extend({
            icon: alertsetting.icon.info,
            title: "提示",
            id: "alert_" + new Date().getTime(),
            maskid: "mask_alert_" + new Date().getTime(),
            buttons: [{ name: "确定", call: function (obj) { obj.data.close() } }],
            mask: true,
            zindex: null,
            maskzindex: null,
            parent: "body",
            width: "61.5%",
            height: "61.5%",
            content: null,
            autoclose: false,
            onclose: function (obj) { },
            application: this.application
        }, option);
        if (option.maskzindex == null) {
            option.maskzindex = this.application.getZindexManager().getTop(option.maskid);
        }
        if (option.zindex == null) {
            option.zindex = this.application.getZindexManager().getTop(option.id);
        }
        var alert = $(alertsetting.template);
        var helper = new $.alertHelper(alert, option);
        alert.helper("alert", helper);
        helper.init();
        return helper;
    };
    this.alert = function (content, callback) {
        var option = { title: "提示", content: content, autoclose: true, onclose: function (obj) {
             if(callback)
             callback(obj.data);
        } };
        if (callback) {
            option.buttons = [
                            { name: "确定", call: function (obj) {  obj.data.close(); } }
                        ]
        }
        this.open(option);
    };

    this.confirm = function (content, okback) {
        var option = { title: "确认提示",
            content: content,
            buttons: [
                            { name: "取消", call: function (obj) { obj.data.close() } },
                            { name: "确定", call: function (obj) { okback(obj.data); obj.data.close(); } }
                        ]
        };
        this.open(option);
    };
    this.error = function (content, callback) {
        var option = { title: "提示", content: content, icon: alertsetting.icon.error };
        if (callback) {
            option.buttons = [
                            { name: "确定", call: function (obj) { callback(obj.data); obj.data.close(); } }
                        ]
        }
        this.open(option);
    };
};


(function ($) {
    $.alertHelper = function (jo, setting) {
        this.jo = jo;
        this.setting = setting;
        this.content = null;
        this.mask = null;
        this.buttonwarp=null;
        var _this = this;
        this.init = function () {
            this.jo.attr("alertid", this.setting.id);
            this.jo.attr("id", this.setting.id);
            this.jo.find("h1").html(this.setting.title);
            //this.jo.css("width", this.setting.width);
            //this.jo.css("height", this.setting.height);
            this.jo.css("z-index", this.setting.zindex);
            this.content = this.jo.find(".msg");
            this.buttonwarp= this.jo.find(".toolbar");
            this.jo.find(".alertcontent").children().addClass(this.setting.icon);
            if(this.setting.buttons){
                for(var i=0;i<this.setting.buttons.length;i++){
                    var button=this.setting.buttons[i];
                    var buttonobj=$(alertsetting.buttontemplate);
                    buttonobj.find("button").html(button.name);
                    buttonobj.css({float:"right",marginLeft:"5px"});       
                    buttonobj.on("click",_this, button.call);            
                    //buttonobj.click(function(){button.call(_this);});
                    this.buttonwarp.append(buttonobj);
                }
            }
            
            if (this.setting.mask) {
                this.mask = $("<div class=\"mask\"></div>");
                this.mask.attr("id", this.setting.maskid);
                this.mask.attr("maskid", this.setting.maskid);
                this.mask.css("z-index", this.setting.maskzindex);
                $(this.setting.parent).append(this.mask);
            }
            $(this.setting.parent).append(this.jo);
            this.html(this.setting.content);
            this.jo.css({top:this.jo.height()*-1+"px"});
            this.jo.animate({top:0},500);
            if(this.setting.autoclose) {
                setTimeout(function() {
                    _this.close();
                }, 2000);
            }
        };
        this.html = function (content) {
            this.content.html(content);
        };
        this.focus = function () {
            var zindex = this.setting.application.getZindexManager().moveTotop(this.setting.id);
            this.jo.css("z-index", zindex);
        };
        this.close = function () {
            var height=this.jo.height();
            this.jo.animate({top:height*-1+"px"},500,function(){   
                if(_this.setting.onclose) {
                    _this.setting.onclose(_this);
                }
                _this.setting.application.getZindexManager().remove(_this.setting.id);
                _this.jo.remove();
                _this.mask && _this.mask.remove();
            });
        };
        
    };
})(jQuery);